package cn.dnui_cr503.dnuifood_16110100503.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.Searchbean;
import cn.dnui_cr503.dnuifood_16110100503.controller.FoodActivity;
import cn.dnui_cr503.dnuifood_16110100503.model.UserModel;

/**
 * Created by chengrui on 2018/12/5.
 */

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Searchbean> mDataList;
    private LayoutInflater mLayoutInflater;
    public SearchAdapter(Context mContext,List mDataList){
        this.mContext=mContext;
        this.mDataList=mDataList;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v=mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_item,viewGroup,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
        final Searchbean entity =mDataList.get(position);
        if(null==entity)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;

        viewHolder.search_name.setText(entity.getFoodname());
        Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(viewHolder.search_pic);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到具体店铺页面
                String str = String.valueOf(mDataList.get(position).getFood_id());
                String str1 = String.valueOf(mDataList.get(position).getFoodname());
                Intent intent = new Intent(mContext,FoodActivity.class);
                intent.putExtra("shop_id",entity.getShop_id());
                intent.putExtra("food_id",str);
                intent.putExtra("foodname",str1);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView search_name;
        ImageView search_pic;
        public ViewHolder(View view) {
            super(view);
            search_pic=view.findViewById(R.id.search_pic);
            search_name=view.findViewById(R.id.search_name);
        }
    }
}
